package com.wanxi.management;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;

public class ManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ManagementSystem managementSystem = new ManagementSystem();

        while (true) {
            Properties properties = managementSystem.loadStudentInformation();
//            System.out.println(properties);
            System.out.println("---------欢迎来到学生管理系统---------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查找学生");
            System.out.println("5 查看所有学生");
            System.out.println("6 退出系统");
            System.out.println("请输入你的选择：");
            int selection = scanner.nextInt();

            switch (selection) {
                case 1:
                    System.out.println("请输入你要添加的学生学号、姓名、年龄、出生日期");
                    String sid = scanner.next();
                    if (!properties.containsKey(sid)) {
                        String name = scanner.next();
                        int age = scanner.nextInt();
                        String birthday = scanner.next();
                        Student addStudent = new Student(sid, name, age, birthday);
                        String key = addStudent.getSid();
                        String value = addStudent.getInfo();
                        properties.setProperty(key, value);
                        managementSystem.writeStudentInformation(properties);
                        System.out.println("添加成功");
                    } else {
                        System.out.println("已有该Id的学生信息，请不要重复添加");
                    }
                    break;

                case 2:
                    System.out.println("请输入需要删除的学生ID： ");
                    String deleteSid = scanner.next();
                    if (properties.containsKey(deleteSid)) {
                        properties.remove(deleteSid);
                        managementSystem.coverStudentInformation(properties);
                        System.out.println("删除成功");
                    } else {
                        System.out.println("没有该Id的学生信息，请先添加");
                    }
                    break;

                case 3:
                    System.out.println("请输入需要修改的学生ID： ");
                    String changeSid = scanner.next();
                    if (properties.containsKey(changeSid)) {
                        System.out.println("请输入需要修改的学生新信息：姓名，年龄，出生年月");
                        String changeName = scanner.next();
                        int changeAge = scanner.nextInt();
                        String changeBirthday = scanner.next();
                        Student changeStudent = new Student(changeSid, changeName, changeAge, changeBirthday);
                        String changeKey = changeStudent.getSid();
                        String changeValue = changeStudent.getInfo();
                        properties.setProperty(changeKey, changeValue);
                        managementSystem.coverStudentInformation(properties);
                        System.out.println("修改成功");
                    } else {
                        System.out.println("没有该Id的学生信息，请先添加");
                    }
                    break;

                case 4:
                    System.out.println("请输入需要查找的学生ID： ");
                    String findSid = scanner.next();
                    if (properties.containsKey(findSid)) {
                        String findInformation = properties.getProperty(findSid);
                        String[] findInformationArray = findInformation.split(" ");
                        String findName = findInformationArray[0];
                        int findAge = Integer.parseInt(findInformationArray[1]);
                        String findBirthday = findInformationArray[2];
                        Student findStudent = new Student(findSid, findName, findAge, findBirthday);
                        System.out.println(findStudent);
                    } else {
                        System.out.println("没有该Id的学生信息，请先添加");
                    }
                    break;

                case 5:
                    Set set = properties.keySet();
                    Iterator iterator = set.iterator();
                    while (iterator.hasNext()) {
                        String theSid = (String) iterator.next();
                        String Information = properties.getProperty(theSid);
                        String[] InformationArray = Information.split(" ");
                        String theName = InformationArray[0];
                        int theAge = Integer.parseInt(InformationArray[1]);
                        String theBirthday = InformationArray[2];
                        Student theStudent = new Student(theSid, theName, theAge, theBirthday);
                        System.out.println(theStudent);
                    }
                    break;

                case 6:
                    managementSystem.coverStudentInformation(properties);
                    System.out.print("正在退出系统 ");
                    for (int i = 3; i >= 0; i--) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.print(i + " ");
                    }
                    System.exit(0);
                    break;

                default:
                    System.out.println("输出操作不正常，请重新输入");
                    break;
            }
        }
    }

    private Properties loadStudentInformation() {
        FileReader fileReader = null;
        Properties properties = null;
        try {
            File file = new File("studentinformation.properties");
//        FileInputStream fileInputStream = new FileInputStream(file);
            fileReader = new FileReader(file);
            properties = new Properties();
            properties.load(fileReader);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null)
                    fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }


    public void writeStudentInformation(Properties properties) {
        FileWriter fileWriter = null;
        try {
            File file = new File("studentinformation.properties");
            fileWriter = new FileWriter(file, true);
            properties.store(fileWriter, "student information");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null)
                    fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void coverStudentInformation(Properties properties) {
        FileWriter fileWriter = null;
        try {
            File file = new File("studentinformation.properties");
            fileWriter = new FileWriter(file);
            properties.store(fileWriter, "student information");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null)
                    fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
